home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_elisp-manual-19.idb / usr / freeware / info / elisp-28.z / elisp-28 (.txt)
GNU Info File  |  1998-05-26  |  48KB  |  840 lines

  1. This is Info file elisp, produced by Makeinfo-1.63 from the input file
  2. elisp.texi.
  3.    This version is the edition 2.4.2 of the GNU Emacs Lisp Reference
  4. Manual.  It corresponds to Emacs Version 19.34.
  5.    Published by the Free Software Foundation 59 Temple Place, Suite 330
  6. Boston, MA  02111-1307  USA
  7.    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software
  8. Foundation, Inc.
  9.    Permission is granted to make and distribute verbatim copies of this
  10. manual provided the copyright notice and this permission notice are
  11. preserved on all copies.
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided that the
  14. entire resulting derived work is distributed under the terms of a
  15. permission notice identical to this one.
  16.    Permission is granted to copy and distribute translations of this
  17. manual into another language, under the above conditions for modified
  18. versions, except that this permission notice may be stated in a
  19. translation approved by the Foundation.
  20.    Permission is granted to copy and distribute modified versions of
  21. this manual under the conditions for verbatim copying, provided also
  22. that the section entitled "GNU General Public License" is included
  23. exactly as in the original, and provided that the entire resulting
  24. derived work is distributed under the terms of a permission notice
  25. identical to this one.
  26.    Permission is granted to copy and distribute translations of this
  27. manual into another language, under the above conditions for modified
  28. versions, except that the section entitled "GNU General Public License"
  29. may be included in a translation approved by the Free Software
  30. Foundation instead of in the original English.
  31. File: elisp,  Node: Special Properties,  Next: Format Properties,  Prev: Property Search,  Up: Text Properties
  32. Properties with Special Meanings
  33. --------------------------------
  34.    Here is a table of text property names that have special built-in
  35. meanings.  The following section lists a few more special property names
  36. that are used to control filling.  All other names have no standard
  37. meaning, and you can use them as you like.
  38. `category'
  39.      If a character has a `category' property, we call it the
  40.      "category" of the character.  It should be a symbol.  The
  41.      properties of the symbol serve as defaults for the properties of
  42.      the character.
  43. `face'
  44.      You can use the property `face' to control the font and color of
  45.      text.  Its value is a face name or a list of face names.  *Note
  46.      Faces::, for more information.  This feature may be temporary; in
  47.      the future, we may replace it with other ways of specifying how to
  48.      display text.
  49. `mouse-face'
  50.      The property `mouse-face' is used instead of `face' when the mouse
  51.      is on or near the character.  For this purpose, "near" means that
  52.      all text between the character and where the mouse is have the same
  53.      `mouse-face' property value.
  54. `local-map'
  55.      You can specify a different keymap for a portion of the text by
  56.      means of a `local-map' property.  The property's value for the
  57.      character after point, if non-`nil', replaces the buffer's local
  58.      map.  *Note Active Keymaps::.
  59. `read-only'
  60.      If a character has the property `read-only', then modifying that
  61.      character is not allowed.  Any command that would do so gets an
  62.      error.
  63.      Insertion next to a read-only character is an error if inserting
  64.      ordinary text there would inherit the `read-only' property due to
  65.      stickiness.  Thus, you can control permission to insert next to
  66.      read-only text by controlling the stickiness.  *Note Sticky
  67.      Properties::.
  68.      Since changing properties counts as modifying the buffer, it is not
  69.      possible to remove a `read-only' property unless you know the
  70.      special trick: bind `inhibit-read-only' to a non-`nil' value and
  71.      then remove the property.  *Note Read Only Buffers::.
  72. `invisible'
  73.      A non-`nil' `invisible' property can make a character invisible on
  74.      the screen.  *Note Invisible Text::, for details.
  75. `intangible'
  76.      If a group of consecutive characters have equal and non-`nil'
  77.      `intangible' properties, then you cannot place point between them.
  78.      If you try to move point forward into the group, point actually
  79.      moves to the end of the group.  If you try to move point backward
  80.      into the group, point actually moves to the start of the group.
  81.      When the variable `inhibit-point-motion-hooks' is non-`nil', the
  82.      `intangible' property is ignored.
  83. `modification-hooks'
  84.      If a character has the property `modification-hooks', then its
  85.      value should be a list of functions; modifying that character
  86.      calls all of those functions.  Each function receives two
  87.      arguments: the beginning and end of the part of the buffer being
  88.      modified.  Note that if a particular modification hook function
  89.      appears on several characters being modified by a single
  90.      primitive, you can't predict how many times the function will be
  91.      called.
  92. `insert-in-front-hooks'
  93. `insert-behind-hooks'
  94.      The operation of inserting text in a buffer also calls the
  95.      functions listed in the `insert-in-front-hooks' property of the
  96.      following character and in the `insert-behind-hooks' property of
  97.      the preceding character.  These functions receive two arguments,
  98.      the beginning and end of the inserted text.  The functions are
  99.      called *after* the actual insertion takes place.
  100.      See also *Note Change Hooks::, for other hooks that are called
  101.      when you change text in a buffer.
  102. `point-entered'
  103. `point-left'
  104.      The special properties `point-entered' and `point-left' record
  105.      hook functions that report motion of point.  Each time point
  106.      moves, Emacs compares these two property values:
  107.         * the `point-left' property of the character after the old
  108.           location, and
  109.         * the `point-entered' property of the character after the new
  110.           location.
  111.      If these two values differ, each of them is called (if not `nil')
  112.      with two arguments: the old value of point, and the new one.
  113.      The same comparison is made for the characters before the old and
  114.      new locations.  The result may be to execute two `point-left'
  115.      functions (which may be the same function) and/or two
  116.      `point-entered' functions (which may be the same function).  In
  117.      any case, all the `point-left' functions are called first,
  118.      followed by all the `point-entered' functions.
  119.      A primitive function may examine characters at various positions
  120.      without moving point to those positions.  Only an actual change in
  121.      the value of point runs these hook functions.
  122.  - Variable: inhibit-point-motion-hooks
  123.      When this variable is non-`nil', `point-left' and `point-entered'
  124.      hooks are not run, and the `intangible' property has no effect.
  125. File: elisp,  Node: Format Properties,  Next: Sticky Properties,  Prev: Special Properties,  Up: Text Properties
  126. Formatted Text Properties
  127. -------------------------
  128.    These text properties affect the behavior of the fill commands.  They
  129. are used for representing formatted text.  *Note Filling::, and *Note
  130. Margins::.
  131. `hard'
  132.      If a newline character has this property, it is a "hard" newline.
  133.      The fill commands do not alter hard newlines and do not move words
  134.      across them.  However, this property takes effect only if the
  135.      variable `use-hard-newlines' is non-`nil'.
  136. `right-margin'
  137.      This property specifies an extra right margin for filling this
  138.      part of the text.
  139. `left-margin'
  140.      This property specifies an extra left margin for filling this part
  141.      of the text.
  142. `justification'
  143.      This property specifies the style of justification for filling
  144.      this part of the text.
  145. File: elisp,  Node: Sticky Properties,  Next: Saving Properties,  Prev: Format Properties,  Up: Text Properties
  146. Stickiness of Text Properties
  147. -----------------------------
  148.    Self-inserting characters normally take on the same properties as the
  149. preceding character.  This is called "inheritance" of properties.
  150.    In a Lisp program, you can do insertion with inheritance or without,
  151. depending on your choice of insertion primitive.  The ordinary text
  152. insertion functions such as `insert' do not inherit any properties.
  153. They insert text with precisely the properties of the string being
  154. inserted, and no others.  This is correct for programs that copy text
  155. from one context to another--for example, into or out of the kill ring.
  156. To insert with inheritance, use the special primitives described in this
  157. section.  Self-inserting characters inherit properties because they work
  158. using these primitives.
  159.    When you do insertion with inheritance, *which* properties are
  160. inherited depends on two specific properties: `front-sticky' and
  161. `rear-nonsticky'.
  162.    Insertion after a character inherits those of its properties that are
  163. "rear-sticky".  Insertion before a character inherits those of its
  164. properties that are "front-sticky".  By default, a text property is
  165. rear-sticky but not front-sticky.  Thus, the default is to inherit all
  166. the properties of the preceding character, and nothing from the
  167. following character.  You can request different behavior by specifying
  168. the stickiness of certain properties.
  169.    If a character's `front-sticky' property is `t', then all its
  170. properties are front-sticky.  If the `front-sticky' property is a list,
  171. then the sticky properties of the character are those whose names are
  172. in the list.  For example, if a character has a `front-sticky' property
  173. whose value is `(face read-only)', then insertion before the character
  174. can inherit its `face' property and its `read-only' property, but no
  175. others.
  176.    The `rear-nonsticky' works the opposite way.  Every property is
  177. rear-sticky by default, so the `rear-nonsticky' property says which
  178. properties are *not* rear-sticky.  If a character's `rear-nonsticky'
  179. property is `t', then none of its properties are rear-sticky.  If the
  180. `rear-nonsticky' property is a list, properties are rear-sticky
  181. *unless* their names are in the list.
  182.    When you insert text with inheritance, it inherits all the
  183. rear-sticky properties of the preceding character, and all the
  184. front-sticky properties of the following character.  The previous
  185. character's properties take precedence when both sides offer different
  186. sticky values for the same property.
  187.    Here are the functions that insert text with inheritance of
  188. properties:
  189.  - Function: insert-and-inherit &rest STRINGS
  190.      Insert the strings STRINGS, just like the function `insert', but
  191.      inherit any sticky properties from the adjoining text.
  192.  - Function: insert-before-markers-and-inherit &rest STRINGS
  193.      Insert the strings STRINGS, just like the function
  194.      `insert-before-markers', but inherit any sticky properties from the
  195.      adjoining text.
  196. File: elisp,  Node: Saving Properties,  Next: Lazy Properties,  Prev: Sticky Properties,  Up: Text Properties
  197. Saving Text Properties in Files
  198. -------------------------------
  199.    You can save text properties in files, and restore text properties
  200. when inserting the files, using these two hooks:
  201.  - Variable: write-region-annotate-functions
  202.      This variable's value is a list of functions for `write-region' to
  203.      run to encode text properties in some fashion as annotations to
  204.      the text being written in the file.  *Note Writing to Files::.
  205.      Each function in the list is called with two arguments: the start
  206.      and end of the region to be written.  These functions should not
  207.      alter the contents of the buffer.  Instead, they should return
  208.      lists indicating annotations to write in the file in addition to
  209.      the text in the buffer.
  210.      Each function should return a list of elements of the form
  211.      `(POSITION . STRING)', where POSITION is an integer specifying the
  212.      relative position in the text to be written, and STRING is the
  213.      annotation to add there.
  214.      Each list returned by one of these functions must be already
  215.      sorted in increasing order by POSITION.  If there is more than one
  216.      function, `write-region' merges the lists destructively into one
  217.      sorted list.
  218.      When `write-region' actually writes the text from the buffer to the
  219.      file, it intermixes the specified annotations at the corresponding
  220.      positions.  All this takes place without modifying the buffer.
  221.  - Variable: after-insert-file-functions
  222.      This variable holds a list of functions for `insert-file-contents'
  223.      to call after inserting a file's contents.  These functions should
  224.      scan the inserted text for annotations, and convert them to the
  225.      text properties they stand for.
  226.      Each function receives one argument, the length of the inserted
  227.      text; point indicates the start of that text.  The function should
  228.      scan that text for annotations, delete them, and create the text
  229.      properties that the annotations specify.  The function should
  230.      return the updated length of the inserted text, as it stands after
  231.      those changes.  The value returned by one function becomes the
  232.      argument to the next function.
  233.      These functions should always return with point at the beginning of
  234.      the inserted text.
  235.      The intended use of `after-insert-file-functions' is for converting
  236.      some sort of textual annotations into actual text properties.  But
  237.      other uses may be possible.
  238.    We invite users to write Lisp programs to store and retrieve text
  239. properties in files, using these hooks, and thus to experiment with
  240. various data formats and find good ones.  Eventually we hope users will
  241. produce good, general extensions we can install in Emacs.
  242.    We suggest not trying to handle arbitrary Lisp objects as property
  243. names or property values--because a program that general is probably
  244. difficult to write, and slow.  Instead, choose a set of possible data
  245. types that are reasonably flexible, and not too hard to encode.
  246.    *Note Format Conversion::, for a related feature.
  247. File: elisp,  Node: Lazy Properties,  Next: Not Intervals,  Prev: Saving Properties,  Up: Text Properties
  248. Lazy Computation of Text Properties
  249. -----------------------------------
  250.    Instead of computing text properties for all the text in the buffer,
  251. you can arrange to compute the text properties for parts of the text
  252. when and if something depends on them.
  253.    The primitive that extracts text from the buffer along with its
  254. properties is `buffer-substring'.  Before examining the properties,
  255. this function runs the abnormal hook `buffer-access-fontify-functions'.
  256.  - Variable: buffer-access-fontify-functions
  257.      This variable holds a list of functions for computing text
  258.      properties.  Before `buffer-substring' copies the text and text
  259.      properties for a portion of the buffer, it calls all the functions
  260.      in this list.  Each of the functions receives two arguments that
  261.      specify the range of the buffer being accessed.  (The buffer
  262.      itself is always the current buffer.)
  263.    The function `buffer-substring-no-properties' does not call these
  264. functions, since it ignores text properties anyway.
  265.    In order to prevent the hook functions from being called more than
  266. once for the same part of the buffer, you can use the variable
  267. `buffer-access-fontified-property'.
  268.  - Variable: buffer-access-fontified-property
  269.      If this value's variable is non-`nil', it is a symbol which is used
  270.      as a text property name.  A non-`nil' value for that text property
  271.      means, "the other text properties for this character have already
  272.      been computed."
  273.      If all the characters in the range specified for `buffer-substring'
  274.      have a non-`nil' value for this property, `buffer-substring' does
  275.      not call the `buffer-access-fontify-functions' functions.  It
  276.      assumes these characters already have the right text properties,
  277.      and just copies the properties they already have.
  278.      The normal way to use this feature is that the
  279.      `buffer-access-fontify-functions' functions add this property, as
  280.      well as others, to the characters they operate on.  That way, they
  281.      avoid being called over and over for the same text.
  282. File: elisp,  Node: Not Intervals,  Prev: Lazy Properties,  Up: Text Properties
  283. Why Text Properties are not Intervals
  284. -------------------------------------
  285.    Some editors that support adding attributes to text in the buffer do
  286. so by letting the user specify "intervals" within the text, and adding
  287. the properties to the intervals.  Those editors permit the user or the
  288. programmer to determine where individual intervals start and end.  We
  289. deliberately provided a different sort of interface in Emacs Lisp to
  290. avoid certain paradoxical behavior associated with text modification.
  291.    If the actual subdivision into intervals is meaningful, that means
  292. you can distinguish between a buffer that is just one interval with a
  293. certain property, and a buffer containing the same text subdivided into
  294. two intervals, both of which have that property.
  295.    Suppose you take the buffer with just one interval and kill part of
  296. the text.  The text remaining in the buffer is one interval, and the
  297. copy in the kill ring (and the undo list) becomes a separate interval.
  298. Then if you yank back the killed text, you get two intervals with the
  299. same properties.  Thus, editing does not preserve the distinction
  300. between one interval and two.
  301.    Suppose we "fix" this problem by coalescing the two intervals when
  302. the text is inserted.  That works fine if the buffer originally was a
  303. single interval.  But suppose instead that we have two adjacent
  304. intervals with the same properties, and we kill the text of one interval
  305. and yank it back.  The same interval-coalescence feature that rescues
  306. the other case causes trouble in this one: after yanking, we have just
  307. one interval.  One again, editing does not preserve the distinction
  308. between one interval and two.
  309.    Insertion of text at the border between intervals also raises
  310. questions that have no satisfactory answer.
  311.    However, it is easy to arrange for editing to behave consistently for
  312. questions of the form, "What are the properties of this character?" So
  313. we have decided these are the only questions that make sense; we have
  314. not implemented asking questions about where intervals start or end.
  315.    In practice, you can usually use the property search functions in
  316. place of explicit interval boundaries.  You can think of them as finding
  317. the boundaries of intervals, assuming that intervals are always
  318. coalesced whenever possible.  *Note Property Search::.
  319.    Emacs also provides explicit intervals as a presentation feature; see
  320. *Note Overlays::.
  321. File: elisp,  Node: Substitution,  Next: Transposition,  Prev: Text Properties,  Up: Text
  322. Substituting for a Character Code
  323. =================================
  324.    The following functions replace characters within a specified region
  325. based on their character codes.
  326.  - Function: subst-char-in-region START END OLD-CHAR NEW-CHAR &optional
  327.           NOUNDO
  328.      This function replaces all occurrences of the character OLD-CHAR
  329.      with the character NEW-CHAR in the region of the current buffer
  330.      defined by START and END.
  331.      If NOUNDO is non-`nil', then `subst-char-in-region' does not
  332.      record the change for undo and does not mark the buffer as
  333.      modified.  This feature is used for controlling selective display
  334.      (*note Selective Display::.).
  335.      `subst-char-in-region' does not move point and returns `nil'.
  336.           ---------- Buffer: foo ----------
  337.           This is the contents of the buffer before.
  338.           ---------- Buffer: foo ----------
  339.           
  340.           (subst-char-in-region 1 20 ?i ?X)
  341.                => nil
  342.           
  343.           ---------- Buffer: foo ----------
  344.           ThXs Xs the contents of the buffer before.
  345.           ---------- Buffer: foo ----------
  346.  - Function: translate-region START END TABLE
  347.      This function applies a translation table to the characters in the
  348.      buffer between positions START and END.
  349.      The translation table TABLE is a string; `(aref TABLE OCHAR)'
  350.      gives the translated character corresponding to OCHAR.  If the
  351.      length of TABLE is less than 256, any characters with codes larger
  352.      than the length of TABLE are not altered by the translation.
  353.      The return value of `translate-region' is the number of characters
  354.      that were actually changed by the translation.  This does not
  355.      count characters that were mapped into themselves in the
  356.      translation table.
  357. File: elisp,  Node: Registers,  Next: Change Hooks,  Prev: Transposition,  Up: Text
  358. Registers
  359. =========
  360.    A register is a sort of variable used in Emacs editing that can hold
  361. a marker, a string, a rectangle, a window configuration (of one frame),
  362. or a frame configuration (of all frames).  Each register is named by a
  363. single character.  All characters, including control and meta characters
  364. (but with the exception of `C-g'), can be used to name registers.
  365. Thus, there are 255 possible registers.  A register is designated in
  366. Emacs Lisp by a character that is its name.
  367.    The functions in this section return unpredictable values unless
  368. otherwise stated.
  369.  - Variable: register-alist
  370.      This variable is an alist of elements of the form `(NAME .
  371.      CONTENTS)'.  Normally, there is one element for each Emacs
  372.      register that has been used.
  373.      The object NAME is a character (an integer) identifying the
  374.      register.  The object CONTENTS is a string, marker, or list
  375.      representing the register contents.  A string represents text
  376.      stored in the register.  A marker represents a position.  A list
  377.      represents a rectangle; its elements are strings, one per line of
  378.      the rectangle.
  379.  - Function: get-register REG
  380.      This function returns the contents of the register REG, or `nil'
  381.      if it has no contents.
  382.  - Function: set-register REG VALUE
  383.      This function sets the contents of register REG to VALUE.  A
  384.      register can be set to any value, but the other register functions
  385.      expect only certain data types.  The return value is VALUE.
  386.  - Command: view-register REG
  387.      This command displays what is contained in register REG.
  388.  - Command: insert-register REG &optional BEFOREP
  389.      This command inserts contents of register REG into the current
  390.      buffer.
  391.      Normally, this command puts point before the inserted text, and the
  392.      mark after it.  However, if the optional second argument BEFOREP
  393.      is non-`nil', it puts the mark before and point after.  You can
  394.      pass a non-`nil' second argument BEFOREP to this function
  395.      interactively by supplying any prefix argument.
  396.      If the register contains a rectangle, then the rectangle is
  397.      inserted with its upper left corner at point.  This means that
  398.      text is inserted in the current line and underneath it on
  399.      successive lines.
  400.      If the register contains something other than saved text (a
  401.      string) or a rectangle (a list), currently useless things happen.
  402.      This may be changed in the future.
  403. File: elisp,  Node: Transposition,  Next: Registers,  Prev: Substitution,  Up: Text
  404. Transposition of Text
  405. =====================
  406.    This subroutine is used by the transposition commands.
  407.  - Function: transpose-regions START1 END1 START2 END2 &optional
  408.           LEAVE-MARKERS
  409.      This function exchanges two nonoverlapping portions of the buffer.
  410.      Arguments START1 and END1 specify the bounds of one portion and
  411.      arguments START2 and END2 specify the bounds of the other portion.
  412.      Normally, `transpose-regions' relocates markers with the transposed
  413.      text; a marker previously positioned within one of the two
  414.      transposed portions moves along with that portion, thus remaining
  415.      between the same two characters in their new position.  However,
  416.      if LEAVE-MARKERS is non-`nil', `transpose-regions' does not do
  417.      this--it leaves all markers unrelocated.
  418. File: elisp,  Node: Change Hooks,  Prev: Registers,  Up: Text
  419. Change Hooks
  420. ============
  421.    These hook variables let you arrange to take notice of all changes in
  422. all buffers (or in a particular buffer, if you make them buffer-local).
  423. See also *Note Special Properties::, for how to detect changes to
  424. specific parts of the text.
  425.    The functions you use in these hooks should save and restore the
  426. match data if they do anything that uses regular expressions;
  427. otherwise, they will interfere in bizarre ways with the editing
  428. operations that call them.
  429.  - Variable: before-change-functions
  430.      This variable holds a list of a functions to call before any buffer
  431.      modification.  Each function gets two arguments, the beginning and
  432.      end of the region that is about to change, represented as
  433.      integers.  The buffer that is about to change is always the
  434.      current buffer.
  435.  - Variable: after-change-functions
  436.      This variable holds a list of a functions to call after any buffer
  437.      modification.  Each function receives three arguments: the
  438.      beginning and end of the region just changed, and the length of
  439.      the text that existed before the change.  (To get the current
  440.      length, subtract the region beginning from the region end.)  All
  441.      three arguments are integers.  The buffer that's about to change
  442.      is always the current buffer.
  443.  - Variable: before-change-function
  444.      This obsolete variable holds one function to call before any buffer
  445.      modification (or `nil' for no function).  It is called just like
  446.      the functions in `before-change-functions'.
  447.  - Variable: after-change-function
  448.      This obsolete variable holds one function to call after any buffer
  449.      modification (or `nil' for no function).  It is called just like
  450.      the functions in `after-change-functions'.
  451.    The four variables above are temporarily bound to `nil' during the
  452. time that any of these functions is running.  This means that if one of
  453. these functions changes the buffer, that change won't run these
  454. functions.  If you do want a hook function to make changes that run
  455. these functions, make it bind these variables back to their usual
  456. values.
  457.    One inconvenient result of this protective feature is that you cannot
  458. have a function in `after-change-functions' or
  459. `before-change-functions' which changes the value of that variable.
  460. But that's not a real limitation.  If you want those functions to change
  461. the list of functions to run, simply add one fixed function to the hook,
  462. and code that function to look in another variable for other functions
  463. to call.  Here is an example:
  464.      (setq my-own-after-change-functions nil)
  465.      (defun indirect-after-change-function (beg end len)
  466.        (let ((list my-own-after-change-functions))
  467.          (while list
  468.            (funcall (car list) beg end len)
  469.            (setq list (cdr list)))))
  470.      (add-hooks 'after-change-functions
  471.                 'indirect-after-change-function)
  472.  - Variable: first-change-hook
  473.      This variable is a normal hook that is run whenever a buffer is
  474.      changed that was previously in the unmodified state.
  475. File: elisp,  Node: Searching and Matching,  Next: Syntax Tables,  Prev: Text,  Up: Top
  476. Searching and Matching
  477. **********************
  478.    GNU Emacs provides two ways to search through a buffer for specified
  479. text: exact string searches and regular expression searches.  After a
  480. regular expression search, you can examine the "match data" to
  481. determine which text matched the whole regular expression or various
  482. portions of it.
  483. * Menu:
  484. * String Search::         Search for an exact match.
  485. * Regular Expressions::   Describing classes of strings.
  486. * Regexp Search::         Searching for a match for a regexp.
  487. * POSIX Regexps::         Searching POSIX-style for the longest match.
  488. * Search and Replace::      Internals of `query-replace'.
  489. * Match Data::            Finding out which part of the text matched
  490.                             various parts of a regexp, after regexp search.
  491. * Searching and Case::    Case-independent or case-significant searching.
  492. * Standard Regexps::      Useful regexps for finding sentences, pages,...
  493.    The `skip-chars...' functions also perform a kind of searching.
  494. *Note Skipping Characters::.
  495. File: elisp,  Node: String Search,  Next: Regular Expressions,  Up: Searching and Matching
  496. Searching for Strings
  497. =====================
  498.    These are the primitive functions for searching through the text in a
  499. buffer.  They are meant for use in programs, but you may call them
  500. interactively.  If you do so, they prompt for the search string; LIMIT
  501. and NOERROR are set to `nil', and REPEAT is set to 1.
  502.  - Command: search-forward STRING &optional LIMIT NOERROR REPEAT
  503.      This function searches forward from point for an exact match for
  504.      STRING.  If successful, it sets point to the end of the occurrence
  505.      found, and returns the new value of point.  If no match is found,
  506.      the value and side effects depend on NOERROR (see below).
  507.      In the following example, point is initially at the beginning of
  508.      the line.  Then `(search-forward "fox")' moves point after the last
  509.      letter of `fox':
  510.           ---------- Buffer: foo ----------
  511.           -!-The quick brown fox jumped over the lazy dog.
  512.           ---------- Buffer: foo ----------
  513.           
  514.           (search-forward "fox")
  515.                => 20
  516.           
  517.           ---------- Buffer: foo ----------
  518.           The quick brown fox-!- jumped over the lazy dog.
  519.           ---------- Buffer: foo ----------
  520.      The argument LIMIT specifies the upper bound to the search.  (It
  521.      must be a position in the current buffer.)  No match extending
  522.      after that position is accepted.  If LIMIT is omitted or `nil', it
  523.      defaults to the end of the accessible portion of the buffer.
  524.      What happens when the search fails depends on the value of
  525.      NOERROR.  If NOERROR is `nil', a `search-failed' error is
  526.      signaled.  If NOERROR is `t', `search-forward' returns `nil' and
  527.      does nothing.  If NOERROR is neither `nil' nor `t', then
  528.      `search-forward' moves point to the upper bound and returns `nil'.
  529.      (It would be more consistent now to return the new position of
  530.      point in that case, but some programs may depend on a value of
  531.      `nil'.)
  532.      If REPEAT is supplied (it must be a positive number), then the
  533.      search is repeated that many times (each time starting at the end
  534.      of the previous time's match).  If these successive searches
  535.      succeed, the function succeeds, moving point and returning its new
  536.      value.  Otherwise the search fails.
  537.  - Command: search-backward STRING &optional LIMIT NOERROR REPEAT
  538.      This function searches backward from point for STRING.  It is just
  539.      like `search-forward' except that it searches backwards and leaves
  540.      point at the beginning of the match.
  541.  - Command: word-search-forward STRING &optional LIMIT NOERROR REPEAT
  542.      This function searches forward from point for a "word" match for
  543.      STRING.  If it finds a match, it sets point to the end of the
  544.      match found, and returns the new value of point.
  545.      Word matching regards STRING as a sequence of words, disregarding
  546.      punctuation that separates them.  It searches the buffer for the
  547.      same sequence of words.  Each word must be distinct in the buffer
  548.      (searching for the word `ball' does not match the word `balls'),
  549.      but the details of punctuation and spacing are ignored (searching
  550.      for `ball boy' does match `ball.  Boy!').
  551.      In this example, point is initially at the beginning of the
  552.      buffer; the search leaves it between the `y' and the `!'.
  553.           ---------- Buffer: foo ----------
  554.           -!-He said "Please!  Find
  555.           the ball boy!"
  556.           ---------- Buffer: foo ----------
  557.           
  558.           (word-search-forward "Please find the ball, boy.")
  559.                => 35
  560.           
  561.           ---------- Buffer: foo ----------
  562.           He said "Please!  Find
  563.           the ball boy-!-!"
  564.           ---------- Buffer: foo ----------
  565.      If LIMIT is non-`nil' (it must be a position in the current
  566.      buffer), then it is the upper bound to the search.  The match
  567.      found must not extend after that position.
  568.      If NOERROR is `nil', then `word-search-forward' signals an error
  569.      if the search fails.  If NOERROR is `t', then it returns `nil'
  570.      instead of signaling an error.  If NOERROR is neither `nil' nor
  571.      `t', it moves point to LIMIT (or the end of the buffer) and
  572.      returns `nil'.
  573.      If REPEAT is non-`nil', then the search is repeated that many
  574.      times.  Point is positioned at the end of the last match.
  575.  - Command: word-search-backward STRING &optional LIMIT NOERROR REPEAT
  576.      This function searches backward from point for a word match to
  577.      STRING.  This function is just like `word-search-forward' except
  578.      that it searches backward and normally leaves point at the
  579.      beginning of the match.
  580. File: elisp,  Node: Regular Expressions,  Next: Regexp Search,  Prev: String Search,  Up: Searching and Matching
  581. Regular Expressions
  582. ===================
  583.    A "regular expression" ("regexp", for short) is a pattern that
  584. denotes a (possibly infinite) set of strings.  Searching for matches for
  585. a regexp is a very powerful operation.  This section explains how to
  586. write regexps; the following section says how to search for them.
  587. * Menu:
  588. * Syntax of Regexps::       Rules for writing regular expressions.
  589. * Regexp Example::          Illustrates regular expression syntax.
  590. File: elisp,  Node: Syntax of Regexps,  Next: Regexp Example,  Up: Regular Expressions
  591. Syntax of Regular Expressions
  592. -----------------------------
  593.    Regular expressions have a syntax in which a few characters are
  594. special constructs and the rest are "ordinary".  An ordinary character
  595. is a simple regular expression that matches that character and nothing
  596. else.  The special characters are `.', `*', `+', `?', `[', `]', `^',
  597. `$', and `\'; no new special characters will be defined in the future.
  598. Any other character appearing in a regular expression is ordinary,
  599. unless a `\' precedes it.
  600.    For example, `f' is not a special character, so it is ordinary, and
  601. therefore `f' is a regular expression that matches the string `f' and
  602. no other string.  (It does *not* match the string `ff'.)  Likewise, `o'
  603. is a regular expression that matches only `o'.
  604.    Any two regular expressions A and B can be concatenated.  The result
  605. is a regular expression that matches a string if A matches some amount
  606. of the beginning of that string and B matches the rest of the string.
  607.    As a simple example, we can concatenate the regular expressions `f'
  608. and `o' to get the regular expression `fo', which matches only the
  609. string `fo'.  Still trivial.  To do something more powerful, you need
  610. to use one of the special characters.  Here is a list of them:
  611. `. (Period)'
  612.      is a special character that matches any single character except a
  613.      newline.  Using concatenation, we can make regular expressions
  614.      like `a.b', which matches any three-character string that begins
  615.      with `a' and ends with `b'.
  616.      is not a construct by itself; it is a suffix operator that means to
  617.      repeat the preceding regular expression as many times as possible.
  618.      In `fo*', the `*' applies to the `o', so `fo*' matches one `f'
  619.      followed by any number of `o's.  The case of zero `o's is allowed:
  620.      `fo*' does match `f'.
  621.      `*' always applies to the *smallest* possible preceding
  622.      expression.  Thus, `fo*' has a repeating `o', not a repeating `fo'.
  623.      The matcher processes a `*' construct by matching, immediately, as
  624.      many repetitions as can be found.  Then it continues with the rest
  625.      of the pattern.  If that fails, backtracking occurs, discarding
  626.      some of the matches of the `*'-modified construct in case that
  627.      makes it possible to match the rest of the pattern.  For example,
  628.      in matching `ca*ar' against the string `caaar', the `a*' first
  629.      tries to match all three `a's; but the rest of the pattern is `ar'
  630.      and there is only `r' left to match, so this try fails.  The next
  631.      alternative is for `a*' to match only two `a's.  With this choice,
  632.      the rest of the regexp matches successfully.
  633.      Nested repetition operators can be extremely slow if they specify
  634.      backtracking loops.  For example, it could take hours for the
  635.      regular expression `\(x+y*\)*a' to match the sequence
  636.      `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz'.  The slowness is because
  637.      Emacs must try each imaginable way of grouping the 35 `x''s before
  638.      concluding that none of them can work.  To make sure your regular
  639.      expressions run fast, check nested repetitions carefully.
  640.      is a suffix operator similar to `*' except that the preceding
  641.      expression must match at least once.  So, for example, `ca+r'
  642.      matches the strings `car' and `caaaar' but not the string `cr',
  643.      whereas `ca*r' matches all three strings.
  644.      is a suffix operator similar to `*' except that the preceding
  645.      expression can match either once or not at all.  For example,
  646.      `ca?r' matches `car' or `cr', but does not match anyhing else.
  647. `[ ... ]'
  648.      `[' begins a "character set", which is terminated by a `]'.  In
  649.      the simplest case, the characters between the two brackets form
  650.      the set.  Thus, `[ad]' matches either one `a' or one `d', and
  651.      `[ad]*' matches any string composed of just `a's and `d's
  652.      (including the empty string), from which it follows that `c[ad]*r'
  653.      matches `cr', `car', `cdr', `caddaar', etc.
  654.      The usual regular expression special characters are not special
  655.      inside a character set.  A completely different set of special
  656.      characters exists inside character sets: `]', `-' and `^'.
  657.      `-' is used for ranges of characters.  To write a range, write two
  658.      characters with a `-' between them.  Thus, `[a-z]' matches any
  659.      lower case letter.  Ranges may be intermixed freely with individual
  660.      characters, as in `[a-z$%.]', which matches any lower case letter
  661.      or `$', `%', or a period.
  662.      To include a `]' in a character set, make it the first character.
  663.      For example, `[]a]' matches `]' or `a'.  To include a `-', write
  664.      `-' as the first character in the set, or put it immediately after
  665.      a range.  (You can replace one individual character C with the
  666.      range `C-C' to make a place to put the `-'.)  There is no way to
  667.      write a set containing just `-' and `]'.
  668.      To include `^' in a set, put it anywhere but at the beginning of
  669.      the set.
  670. `[^ ... ]'
  671.      `[^' begins a "complement character set", which matches any
  672.      character except the ones specified.  Thus, `[^a-z0-9A-Z]' matches
  673.      all characters *except* letters and digits.
  674.      `^' is not special in a character set unless it is the first
  675.      character.  The character following the `^' is treated as if it
  676.      were first (thus, `-' and `]' are not special there).
  677.      Note that a complement character set can match a newline, unless
  678.      newline is mentioned as one of the characters not to match.
  679.      is a special character that matches the empty string, but only at
  680.      the beginning of a line in the text being matched.  Otherwise it
  681.      fails to match anything.  Thus, `^foo' matches a `foo' that occurs
  682.      at the beginning of a line.
  683.      When matching a string instead of a buffer, `^' matches at the
  684.      beginning of the string or after a newline character `\n'.
  685.      is similar to `^' but matches only at the end of a line.  Thus,
  686.      `x+$' matches a string of one `x' or more at the end of a line.
  687.      When matching a string instead of a buffer, `$' matches at the end
  688.      of the string or before a newline character `\n'.
  689.      has two functions: it quotes the special characters (including
  690.      `\'), and it introduces additional special constructs.
  691.      Because `\' quotes special characters, `\$' is a regular
  692.      expression that matches only `$', and `\[' is a regular expression
  693.      that matches only `[', and so on.
  694.      Note that `\' also has special meaning in the read syntax of Lisp
  695.      strings (*note String Type::.), and must be quoted with `\'.  For
  696.      example, the regular expression that matches the `\' character is
  697.      `\\'.  To write a Lisp string that contains the characters `\\',
  698.      Lisp syntax requires you to quote each `\' with another `\'.
  699.      Therefore, the read syntax for a regular expression matching `\'
  700.      is `"\\\\"'.
  701.    *Please note:* For historical compatibility, special characters are
  702. treated as ordinary ones if they are in contexts where their special
  703. meanings make no sense.  For example, `*foo' treats `*' as ordinary
  704. since there is no preceding expression on which the `*' can act.  It is
  705. poor practice to depend on this behavior; quote the special character
  706. anyway, regardless of where it appears.
  707.    For the most part, `\' followed by any character matches only that
  708. character.  However, there are several exceptions: characters that,
  709. when preceded by `\', are special constructs.  Such characters are
  710. always ordinary when encountered on their own.  Here is a table of `\'
  711. constructs:
  712.      specifies an alternative.  Two regular expressions A and B with
  713.      `\|' in between form an expression that matches anything that
  714.      either A or B matches.
  715.      Thus, `foo\|bar' matches either `foo' or `bar' but no other string.
  716.      `\|' applies to the largest possible surrounding expressions.
  717.      Only a surrounding `\( ... \)' grouping can limit the grouping
  718.      power of `\|'.
  719.      Full backtracking capability exists to handle multiple uses of
  720.      `\|'.
  721. `\( ... \)'
  722.      is a grouping construct that serves three purposes:
  723.        1. To enclose a set of `\|' alternatives for other operations.
  724.           Thus, the regular expression `\(foo\|bar\)x' matches either
  725.           `foox' or `barx'.
  726.        2. To enclose an expression for a suffix operator such as `*' to
  727.           act on.  Thus, `ba\(na\)*' matches `bananana', etc., with any
  728.           (zero or more) number of `na' strings.
  729.        3. To record a matched substring for future reference.
  730.      This last application is not a consequence of the idea of a
  731.      parenthetical grouping; it is a separate feature that happens to be
  732.      assigned as a second meaning to the same `\( ... \)' construct
  733.      because there is no conflict in practice between the two meanings.
  734.      Here is an explanation of this feature:
  735. `\DIGIT'
  736.      matches the same text that matched the DIGITth occurrence of a `\(
  737.      ... \)' construct.
  738.      In other words, after the end of a `\( ... \)' construct.  the
  739.      matcher remembers the beginning and end of the text matched by that
  740.      construct.  Then, later on in the regular expression, you can use
  741.      `\' followed by DIGIT to match that same text, whatever it may
  742.      have been.
  743.      The strings matching the first nine `\( ... \)' constructs
  744.      appearing in a regular expression are assigned numbers 1 through 9
  745.      in the order that the open parentheses appear in the regular
  746.      expression.  So you can use `\1' through `\9' to refer to the text
  747.      matched by the corresponding `\( ... \)' constructs.
  748.      For example, `\(.*\)\1' matches any newline-free string that is
  749.      composed of two identical halves.  The `\(.*\)' matches the first
  750.      half, which may be anything, but the `\1' that follows must match
  751.      the same exact text.
  752.      matches any word-constituent character.  The editor syntax table
  753.      determines which characters these are.  *Note Syntax Tables::.
  754.      matches any character that is not a word constituent.
  755. `\sCODE'
  756.      matches any character whose syntax is CODE.  Here CODE is a
  757.      character that represents a syntax code: thus, `w' for word
  758.      constituent, `-' for whitespace, `(' for open parenthesis, etc.
  759.      *Note Syntax Tables::, for a list of syntax codes and the
  760.      characters that stand for them.
  761. `\SCODE'
  762.      matches any character whose syntax is not CODE.
  763.    The following regular expression constructs match the empty
  764. string--that is, they don't use up any characters--but whether they
  765. match depends on the context.
  766.      matches the empty string, but only at the beginning of the buffer
  767.      or string being matched against.
  768.      matches the empty string, but only at the end of the buffer or
  769.      string being matched against.
  770.      matches the empty string, but only at point.  (This construct is
  771.      not defined when matching against a string.)
  772.      matches the empty string, but only at the beginning or end of a
  773.      word.  Thus, `\bfoo\b' matches any occurrence of `foo' as a
  774.      separate word.  `\bballs?\b' matches `ball' or `balls' as a
  775.      separate word.
  776.      matches the empty string, but *not* at the beginning or end of a
  777.      word.
  778.      matches the empty string, but only at the beginning of a word.
  779.      matches the empty string, but only at the end of a word.
  780.    Not every string is a valid regular expression.  For example, a
  781. string with unbalanced square brackets is invalid (with a few
  782. exceptions, such as `[]]'), and so is a string that ends with a single
  783. `\'.  If an invalid regular expression is passed to any of the search
  784. functions, an `invalid-regexp' error is signaled.
  785.  - Function: regexp-quote STRING
  786.      This function returns a regular expression string that matches
  787.      exactly STRING and nothing else.  This allows you to request an
  788.      exact string match when calling a function that wants a regular
  789.      expression.
  790.           (regexp-quote "^The cat$")
  791.                => "\\^The cat\\$"
  792.      One use of `regexp-quote' is to combine an exact string match with
  793.      context described as a regular expression.  For example, this
  794.      searches for the string that is the value of `string', surrounded
  795.      by whitespace:
  796.           (re-search-forward
  797.            (concat "\\s-" (regexp-quote string) "\\s-"))
  798. File: elisp,  Node: Regexp Example,  Prev: Syntax of Regexps,  Up: Regular Expressions
  799. Complex Regexp Example
  800. ----------------------
  801.    Here is a complicated regexp, used by Emacs to recognize the end of a
  802. sentence together with any whitespace that follows.  It is the value of
  803. the variable `sentence-end'.
  804.    First, we show the regexp as a string in Lisp syntax to distinguish
  805. spaces from tab characters.  The string constant begins and ends with a
  806. double-quote.  `\"' stands for a double-quote as part of the string,
  807. `\\' for a backslash as part of the string, `\t' for a tab and `\n' for
  808. a newline.
  809.      "[.?!][]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*"
  810.    In contrast, if you evaluate the variable `sentence-end', you will
  811. see the following:
  812.      sentence-end
  813.      =>
  814.      "[.?!][]\"')}]*\\($\\| $\\|  \\|  \\)[
  815.      ]*"
  816. In this output, tab and newline appear as themselves.
  817.    This regular expression contains four parts in succession and can be
  818. deciphered as follows:
  819. `[.?!]'
  820.      The first part of the pattern is a character set that matches any
  821.      one of three characters: period, question mark, and exclamation
  822.      mark.  The match must begin with one of these three characters.
  823. `[]\"')}]*'
  824.      The second part of the pattern matches any closing braces and
  825.      quotation marks, zero or more of them, that may follow the period,
  826.      question mark or exclamation mark.  The `\"' is Lisp syntax for a
  827.      double-quote in a string.  The `*' at the end indicates that the
  828.      immediately preceding regular expression (a character set, in this
  829.      case) may be repeated zero or more times.
  830. `\\($\\| $\\|\t\\|  \\)'
  831.      The third part of the pattern matches the whitespace that follows
  832.      the end of a sentence: the end of a line, or a tab, or two spaces.
  833.      The double backslashes mark the parentheses and vertical bars as
  834.      regular expression syntax; the parentheses delimit a group and the
  835.      vertical bars separate alternatives.  The dollar sign is used to
  836.      match the end of a line.
  837. `[ \t\n]*'
  838.      Finally, the last part of the pattern matches any additional
  839.      whitespace beyond the minimum needed to end a sentence.
  840.